package com.androidme.app.examples;

import android.os.Message;
import android.view.View;
import com.androidme.R;
import com.androidme.app.framework.base.AndroidMeActivity;
import com.androidme.app.framework.base.AndroidMeApplication;
import com.androidme.app.framework.base.AndroidMeDownloadTask;
import com.androidme.app.framework.util.MD5Util;
import com.androidme.app.framework.widget.advert.AdvertInfo;
import com.androidme.app.framework.widget.advert.AdvertModel;
import com.androidme.app.framework.widget.advert.AdvertView;

import java.io.File;
import java.util.ArrayList;

public class TestAdvertViewActivity extends AndroidMeActivity {
    protected AdvertView advertView;
    private AdvertInfo advInfo;
    private ArrayList<AdvertModel> advDataList = new ArrayList<AdvertModel>();
    private ArrayList<AdvertModel> advList = new ArrayList<AdvertModel>();
    private boolean isNeedFoot = false;
    protected int advertType = AdvertModel.ADVERT_TYPE_HOME;
    private static final int REQUEST_AVDERT_DATA_FINISHED = 10;
    private static final int REQUEST_AVDERT_INFO_FINISHED = 11;

    @Override
    protected void onPrepareData() {
        //prepare advert info
        //prepare advert data
    }

    @Override
    protected void onRefresh() {
        advList = advDataList;
        if (advList != null && advList.size() > 0) {// only init one time
            if (isNeedFoot)
                advertView.setFootVisibility(View.VISIBLE);
            advertView.initAdvertList(advList);
        } else {
            advertView.setAdvertVisibility(View.GONE);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (advertView != null) {
            advertView.clear();
        }
        advDataList.clear();
        advDataList = null;
    }

    protected void initAdvertView() {
        if (advertView == null)
            advertView = (AdvertView) findViewById(R.id.advert_view);
        if (advertView != null)
            advertView.setActivity(this);
        if (advDataList != null)
            advDataList.clear();
        if (advList != null)
            advList.clear();
    }

    public void setAdvertType(int type, boolean needFoot) {
        advertType = type;
        isNeedFoot = needFoot;
    }

    protected void onHandleMessage(Message msg) {
        switch (msg.what) {
            case REQUEST_AVDERT_DATA_FINISHED:
                for (AdvertModel advert : (ArrayList<AdvertModel>) advDataList) {
                    if (advert.getPic().equals("") == false) {
                        File file = new File(AndroidMeApplication.FILE_PIC_ADVERT,
                                getImageName(advert.getPic()));
                        if (!file.exists()) {
                            doAsync(new AndroidMeDownloadTask(getClass().getName(),file, advert.getPic()) {
                                @Override
                                protected void onHTTPException() {
                                }

                                @Override
                                protected void onHttpResponseError(int statusCode) {
                                }

                                @Override
                                protected void onSuccess() {
                                }

                                @Override
                                protected void onFailure() {
                                }

                                @Override
                                protected void onStart() {
                                }
                            });
                        }
                    }
                }
                refresh();
                break;

            case REQUEST_AVDERT_INFO_FINISHED:
                if (advInfo != null && advertView != null) {
                    advertView.setAdvertInfo(advInfo);
                }
                break;
        }
    }

    private static String getImageName(String imgUri) {
        return MD5Util.md5(imgUri);
    }
}
